<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-header">
        <h2>{{ isLogin ? '登录' : '注册' }}</h2>
      </div>
      
      <el-tabs v-model="activeTab" class="login-tabs">
        <el-tab-pane label="登录" name="login">
          <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login-form">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="注册" name="register">
          <el-form :model="registerForm" :rules="registerRules" ref="registerFormRef" class="register-form">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" show-password />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="el-icon-check" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="register-button" @click="handleRegister" :loading="loading">注册</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request' // 引入axios配置

export default {
  name: 'LoginView',
  setup() {
    // 状态管理
    const activeTab = ref('login')
    const isLogin = ref(true)
    const loading = ref(false)
    const loginFormRef = ref(null)
    const registerFormRef = ref(null)
    
    // 登录表单
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    // 注册表单
    const registerForm = reactive({
      username: '',
      password: '',
      confirmPassword: ''
    })
    
    // 登录表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
      ]
    }
    
    // 注册表单验证规则
    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { validator: (rule, value, callback) => {
            if (value !== registerForm.password) {
              callback(new Error('两次输入的密码不一致'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
      ]
    }
    
    // 处理登录
    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        loading.value = true
        
        // 模拟登录请求
        // 实际项目中应该使用 request.post('/api/login', loginForm)
        console.log('登录请求数据:', loginForm)
        
        // 模拟成功响应
        setTimeout(() => {
          loading.value = false
          ElMessage.success('登录成功')
          // 这里可以添加路由跳转逻辑
          // router.push('/')
        }, 1000)
      } catch (error) {
        loading.value = false
        console.error('登录失败:', error)
        if (error !== false) {
          ElMessage.error('登录失败，请稍后重试')
        }
      }
    }
    
    // 处理注册
    const handleRegister = async () => {
      try {
        await registerFormRef.value.validate()
        loading.value = true
        
        // 模拟注册请求
        // 实际项目中应该使用 request.post('/api/register', registerForm)
        console.log('注册请求数据:', registerForm)
        
        // 模拟成功响应
        setTimeout(() => {
          loading.value = false
          ElMessage.success('注册成功')
          // 注册成功后切换到登录标签
          activeTab.value = 'login'
        }, 1000)
      } catch (error) {
        loading.value = false
        console.error('注册失败:', error)
        if (error !== false) {
          ElMessage.error('注册失败，请稍后重试')
        }
      }
    }
    
    return {
      activeTab,
      isLogin,
      loading,
      loginFormRef,
      registerFormRef,
      loginForm,
      registerForm,
      loginRules,
      registerRules,
      handleLogin,
      handleRegister
    }
  }
}
</script>

<style>
/* 全局样式重置，确保没有默认边距 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  width: 100%;
  overflow-x: hidden;
}

#app {
  height: 100%;
  width: 100%;
}
</style>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
}

.login-form-wrapper {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 2;
}

.login-header {
  background-color: #667eea;
  color: white;
  padding: 20px;
  text-align: center;
}

.login-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
}

.login-tabs {
  padding: 20px;
}

.login-form,
.register-form {
  width: 100%;
}

.login-button,
.register-button {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  background-color: #667eea;
  border-color: #667eea;
}

.login-button:hover,
.register-button:hover {
  background-color: #5a6fd8;
  border-color: #5a6fd8;
}
</style>